<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE事件流演示</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        #eventLog {
            width: 100%;
            height: 400px;
            border: 1px solid #ccc;
            padding: 10px;
            margin-top: 20px;
            overflow-y: auto;
            background-color: #f9f9f9;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
        .status {
            margin-top: 10px;
            padding: 10px;
            border-radius: 4px;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>
<body>
    <h1>SSE事件流演示</h1>
    <div id="status" class="status disconnected">未连接</div>
    <div id="eventLog"></div>

    <script>
        const eventLog = document.getElementById('eventLog');
        const statusDiv = document.getElementById('status');
        let eventSource = null;

        function connectSSE() {
            if (eventSource) {
                eventSource.close();
            }

            eventSource = new EventSource('/sse');

            eventSource.onopen = () => {
                statusDiv.className = 'status connected';
                statusDiv.textContent = '已连接';
            };

            eventSource.onmessage = (event) => {
                eventLog.textContent += event.data;
                eventLog.scrollTop = eventLog.scrollHeight;
            };

            eventSource.onerror = () => {
                statusDiv.className = 'status disconnected';
                statusDiv.textContent = '连接断开，正在重连...';
                eventSource.close();
                setTimeout(connectSSE, 5000);
            };
        }

        // 页面加载时自动连接
        connectSSE();
    </script>
</body>
</html> 